﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Elysium.Demo;
using SIIE_ERP.Codigo;
using SIIE_ERP.View;
using SIIE_ERP.BaseDatos;
using Elysium.Demo.Controls;

namespace Elysium.Demo.Informes
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class Seleccion_Compras_AnalisisPrecios_WPF : UserControl
    {
        public UsuarioActivo _sesion;
        private InventariosExistenciasViewModel _MiViewModel;

        public int Id_Articulo;
        private int Id_Proveedor;

        bool IsReportEmbedded;

        public Seleccion_Compras_AnalisisPrecios_WPF(ref UsuarioActivo usuario, bool IsReportEmbedded = false)
        {
            this._sesion = usuario;
            Init();
            

            InitializeComponent();
            _MiViewModel = (InventariosExistenciasViewModel)this.DataContext;
            
            
            CargarCombos();
            
            

            CargarDatos();

            if (IsReportEmbedded)
                ButtonRegresar.Visibility = System.Windows.Visibility.Hidden;
            this.IsReportEmbedded = IsReportEmbedded;
        }

        public Seleccion_Compras_AnalisisPrecios_WPF()
        {
            
            Init();


            InitializeComponent();
            _MiViewModel = (InventariosExistenciasViewModel)this.DataContext;

        }

        public void CargarDatos()
        {
            if (PartidasDataGrid != null && _MiViewModel != null)
            {
                SIIE_ERP.BaseDatos.Inventarios.Catalogos.dbo_Cat_Articulo Cat_Articulo = new SIIE_ERP.BaseDatos.Inventarios.Catalogos.dbo_Cat_Articulo(_sesion.RFC, _sesion.UserName);
                DataTable dt_Datos = Cat_Articulo.Get_Articulos_AnalisisCostos(Id_Articulo, Id_Proveedor);

                PartidasDataGrid.Items.Clear();
                _MiViewModel.DatosGraph.Clear();

                for (int i = 0; i < dt_Datos.Rows.Count; i++)
                {
                    PartidasDataGrid.Items.Add(new GridAnalisis() { Nombre_Comercial = dt_Datos.Rows[i]["Nombre_Comercial"].ToString(), Precio_Unitario_Final_Pesos = Double.Parse(dt_Datos.Rows[i]["Precio_Unitario_Final_Pesos"].ToString()), Documento = dt_Datos.Rows[i]["Documento"].ToString(), Fecha_Documento = dt_Datos.Rows[i]["Fecha_Documento"].ToString() });
                    _MiViewModel.DatosGraph.Add(new TestClass() { Grupo = dt_Datos.Rows[i]["Nombre_Comercial"].ToString(), Valor = Double.Parse(dt_Datos.Rows[i]["Precio_Unitario_Final_Pesos"].ToString()) });
                }
            }
        }

        public void Init()
        {
            Id_Articulo = -1;
            Id_Proveedor = -1;
        }

        public void CargarCombos()
        {            
            ComboBoxUI.ComboArticulos_AutoComplete_Inventarios(ref Combo_ArticulosAutoComplete, this._sesion, "");
            ComboBoxUI.ComboProveedores_AutoComplete(ref Combo_ProveedoresAutoComplete, this._sesion, "");
        }

        private void ButtonRegresar_Click(object sender, RoutedEventArgs e)
        {
            Viewbox Aux = (Viewbox)this.Parent;
            Aux.Child = null;
            Aux.Child = new Menu_Informes(ref _sesion);           
        }

        private void ButtonRegresar_Click_1(object sender, RoutedEventArgs e)
        {
            Viewbox Aux = (Viewbox)this.Parent;
            Aux.Child = null;
            Aux.Child = new Menu_Informes(ref _sesion);  
        }

        private void Combo_ArticulosAutoComplete_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (Combo_ArticulosAutoComplete.SelectedItem != null)
                Id_Articulo = Int32.Parse(((CAutocomplete)Combo_ArticulosAutoComplete.SelectedItem).Id);
            else
                Id_Articulo = -1;

            CargarDatos();
        }        

        private void Combo_ClientesAutoComplete_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (Combo_ProveedoresAutoComplete.SelectedItem != null)
                Id_Proveedor = Int32.Parse(((CAutocomplete)Combo_ProveedoresAutoComplete.SelectedItem).Id);
            else
                Id_Proveedor = -1;
            
            CargarDatos();
        }

        private void Button_DeseleccionProveedor_Click_1(object sender, RoutedEventArgs e)
        {
            Combo_ProveedoresAutoComplete.SelectedItem = null;
        }

        private void Button_DeseleccionArticulo_Click_1(object sender, RoutedEventArgs e)
        {
            Combo_ArticulosAutoComplete.SelectedItem = null;
        }
    }

    public class GridAnalisis
    {
        public string Nombre_Comercial { get; set; }
        public string Documento { get; set; }
        public string Fecha_Documento { get; set; }
        public double Precio_Unitario_Final_Pesos { get; set; }
    }
}

